package com.jr.controller;

import com.jr.pojo.CareProvider;
import com.jr.service.CareProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/providers")
@CrossOrigin(origins = "*")
public class CareProviderController {

    @Autowired
    private CareProviderService careProviderService;

    /**
     * 获取所有已审核的服务商
     */
    @GetMapping("/all")
    @ResponseBody
    public Map<String, Object> getAllProviders() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CareProvider> providers = careProviderService.getAllApprovedProviders();
            result.put("success", true);
            result.put("providers", providers);
            result.put("count", providers.size());
            result.put("message", "成功获取所有服务商");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务商失败: " + e.getMessage());
            System.err.println("获取服务商失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID获取服务商详情
     */
    @GetMapping("/{providerId}")
    @ResponseBody
    public Map<String, Object> getProviderById(@PathVariable Integer providerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            CareProvider provider = careProviderService.getProviderById(providerId);
            if (provider != null) {
                result.put("success", true);
                result.put("provider", provider);
                result.put("message", "成功获取服务商信息");
            } else {
                result.put("success", false);
                result.put("message", "服务商不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务商失败: " + e.getMessage());
            System.err.println("获取服务商失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据服务区域获取服务商
     */
    @GetMapping("/area/{area}")
    @ResponseBody
    public Map<String, Object> getProvidersByArea(@PathVariable String area) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CareProvider> providers = careProviderService.getProvidersByArea(area);
            result.put("success", true);
            result.put("providers", providers);
            result.put("count", providers.size());
            result.put("message", "成功获取区域服务商");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取区域服务商失败: " + e.getMessage());
            System.err.println("获取区域服务商失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取评分最高的服务商
     */
    @GetMapping("/top-rated")
    @ResponseBody
    public Map<String, Object> getTopRatedProviders(@RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CareProvider> providers = careProviderService.getTopRatedProviders(limit);
            result.put("success", true);
            result.put("providers", providers);
            result.put("count", providers.size());
            result.put("message", "成功获取高评分服务商");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取高评分服务商失败: " + e.getMessage());
            System.err.println("获取高评分服务商失败: " + e.getMessage());
        }
        return result;
    }
}
